<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <style>
        img{
            width:60px;
        }
    </style>
</head>
<body>
    <div id="app">
        <h2>豆瓣Top250</h2>
        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th>编号</th>
                    <th>名字</th>
                    <th>海报</th>
                    <th>评分</th>
                    <th>是否收藏</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr @click="handleToggle(item._id)" v-for="item of movies">
                    <td>{{item._id}}</td>
                    <td>{{item.title}}</td>
                    <td><img :src="item.pic" alt=""></td>
                    <td>{{item.rating}}</td>
                    <td>
                        <img 
                        @click = "handleCollect(item._id)"
                        :src="item.collected?'images/collect.png':'images/cancel.png'" alt="">
                    </td>
                    <td>
                    <button @click="handleDelete(item._id)"  type="button" class="btn btn-danger">删除</button>
                    </td>
                </tr>
            </tbody>
        </table>
        <!-- 分页 -->
        <nav aria-label="Page navigation" class="text-center">
            <ul class="pagination pagination-lg">
              <li>
                <a href="javascript:void(0)" aria-label="Previous">
                  <span aria-hidden="true">&laquo;</span>
                </a>
              </li>
                <li  v-for="item of pages">
                  <a @click="doPage(item)" href="javascript:void(0)">{{item}}</a>
                </li>
              <li>
                <a href="javascript:void(0)" aria-label="Next">
                  <span aria-hidden="true">&raquo;</span>
                </a>
              </li>
            </ul>
          </nav>
    </div>
    <script>
        new Vue({
            el:"#app",
            data:{
                movies:[],
                total:""
            },
            mounted() {
                $.ajax({
                    url:"http://localhost:8080/top250",
                    success:res=>{
                        this.movies = res.res
                        this.total = res.total;
                    }
                })
            },
            methods: {
               async handleCollect(id){
                   var movies = this.movies;
                   var item = movies.find(item=>{
                       return item._id == id;
                   })
                   if(item.collected){
                       item.collected = false;
                       await $.ajax({url:"http://localhost:8080/collect/cancel",data:{id},type:"post"})
                   }else{
                       item.collected = true;
                       await $.ajax({url:"http://localhost:8080/collect",data:{id},type:"post"})
                   }
                   this.movies = movies;
               },
               async handleDelete(id){
                     var movies = this.movies.filter(item=>{
                       return  item._id != id;
                     })
                     await $.ajax({url:"http://localhost:8080/delete",data:{id},type:"post"});
                     this.movies = movies;
               },
               async doPage(num){
                //    console.log(num)//点击即可出page页面1，2，
                   var start = (num-1)*5;
                //    console.log(start)   //输出为0，5，10，
                   $.ajax({
                    url:`http://localhost:8080/top250?start=${start}`,
                    success:res=>{
                        this.movies = res.res
                        this.total = res.total;
                    }
                })
               },


               async handleToggle(id){
                //    console.log(id)
                   location.href = `html/detail.html?id=${id}`
               }
            },
            //计算属性
            computed:{
                pages:function(){
                    
                    // 23
                    // [0,5,10,15,20]
                    // （item+5)/5
                    // [1,2,3,4,5] 
                    
                    var total = this.total;
                    var arr = [];
                    for(var i=0;i<total;i+=5){
                        arr.push(i);
                    }
                    // console.log(arr)//start 0,5,10
                    // return arr;
                    var res = arr.map(item=>{
                        return (item+5)/5
                    })
                    return res;
                }
            }
        })
    </script>
</body>
</html>